<!--
////////////////////////////////////////////////////////////////
PLEASE NOTE THAT

update_qty.ctp is exact same code as orders/add.ctp.

This is because, when the user comes to the payment page for the very first time, addctp is used to display the putput.

However, when the user changes the quantity, the ajax request is sent to waiters controller,
which in turn updates the order in the Session through WritingPad component,
and then sends updated order details which uses waiters/update_qty.ctp and
the output gets inserted in the div tag main_body_container.

////////////////////////////////////////////////////////////////
-->


<div class="orders form">
    <?php echo $this->Form->create('Order'); ?>
    <fieldset>
        <legend><?php __('Place Order'); ?></legend>
        <?php
        if (isset($ordered_items_list) && is_array($ordered_items_list)) {

            if (count($ordered_items_list) > 0) {
                e("<table border=1>");
//debug($output);
                $total = 0.00;
                foreach ($ordered_items_list as $menu_item_id => $detailsArray) {
                    //$detailsArray data structure is defined in  writingpad_controller
                    $name = $detailsArray["from_writingpad_menu_item_name"];
                    $qty = $detailsArray["from_writingpad_quantity"];
                    $price = $detailsArray["from_writingpad_price"];
                    $itemCost = $qty * $price;
                    $total = $total + ($itemCost);
                    $txtBoxId = $menu_item_id;
                    $options = array('id' => $txtBoxId, 'value' => $qty, 'name' => $menu_item_id, 'label' => false);
                    $qtyTag = $form->input($txtBoxId, $options);
                    $qtyAjaxText = $ajax->observeField($txtBoxId,
                                    array(
                                        'url' => array(
                                            'controller' => 'waiters',
                                            'action' => 'updateOrder'),
                                        'frequency' => 0.2
                                        //, 'update' => 'right_nav_container_ajax'
                                        , 'update' => 'main_body_container'
                                    )
                    );

                    //print the Bill Row
                    e("<tr>");
                    e("<td>" . $name . "</td>" . "<td>" . $qtyTag . $qtyAjaxText . "</td>" . "<td>$" . $itemCost . "</td>");
                    e("</tr>");

                    //Create a hidden field of menu_item_id>quantity
                    //So that we can use it to add it to ordder_line_items table
                    echo $form->input($menu_item_id,
                            array(
                                'type' => 'hidden',
                                'value' => $qty,
                            )
                    );
                }
                e("<tr>");
                e("<td colspan=2>" . "Total" . "</td>" . "<td>$" . $total . "</td>");
                e("</tr>");
                e("<tr>");
                e("<td>");
                echo "Notes";
                e("</td>");
                e("<td colspan=2>");
                echo $form->textarea('notes');
                e("</td>");
                e("</tr>");

                e("</table>");
        ?>
            </fieldset>
        <?php
                echo $this->Form->end(__('Pay', true));
            } else {
                //$this->controller->Session->setFlash("Please add menu items to the order.");
//                $url = "/menu_items/showmenu/";
//                $this->redirect($url);
//                return;
                e("No items in the order. Please go to Menu Card and choose the items.");
            }
        }
//        else {
////                $this->Session->setFlash("Please add menu items to the order.");
////                $url = "/menu_items/showmenu/";
////                $this->Controller->redirect($url);
////                return;
//                e("No items in the order");
//            }
        ?>
</div>